import java.util.ArrayList;
import java.util.List;

public class Main {
    List<List<Integer>> ret;
    List<Integer> path;
    int[] c;
    int len;
    int target;

    public List<List<Integer>> combinationSum2(int[] candidates, int _target) {
        ret = new ArrayList<>();
        path = new ArrayList<>();
        len = candidates.length;
        c = candidates;
        target = _target;
        dfs(0,0);
        return ret;
    }

    public void dfs(int pos,int sum) {
        if(pos == len || sum == target) {
            if(sum == target) ret.add(new ArrayList(path));
            return;
        }

        if(sum + c[pos] <= target) {
            path.add(c[pos]);
            dfs(pos+1,sum + c[pos]);
            path.remove(path.size()-1);
        }
        dfs(pos+1,sum);
    }

    public static void main(String[] args) {
        int[] a = {10,1,2,7,6,1,5};
        Main main1 = new Main();
        main1.combinationSum2(a,8);
    }
}
